$(function () {
    layui.use(['jquery', 'table'], function () {
        var table = layui.table;
        var $ = layui.jquery;
        //渲染表格
        table.render({
            elem: "#noticeTable",//渲染的表格的id
            url: "/bbs/getNotice.action",//请求后台的接口
            cols: [[//表头
                {type:'numbers',title:'编号',width:90,align:'center'},
                {field:'notice_id',title:'公告ID',width:120,align:'center'},
                {field:'notice_title',title:'公告标题',width:190,align:'center'},
                {field:'notice_content',title:'公告内容', width:300,align:'center'},
                {field:'notice_date',title:'公告日期',width:180,align:'center'},
                {field:'notice_author_name',title:'作者',width:180,align:'center'},
                {title:'操作',toolbar:'#userOperation',width:180,align:'center'},
            ]],
            page: false//是否开启分页
        });

        //表格的重载
        function reloadNoticeTable(){
            table.reload("noticeTable",{
                where:{
                    link_id:$("notice_id").val(),
                    link_name:$("notice_title").val(),
                    link_content:$("notice_content").val(),
                    notice_date:$("notice_date").val(),
                    notice_author_name:$("notice_author_name").val(),
                }
            },'data');
        }

        //监听表格的工具栏的事件
        table.on('tool(noticeTable)',function (obj) {
            var data = obj.data;
            if(obj.event == 'updateNotice'){
                alert("编辑");
                //changeUserType(data.u_id);
            }else if(obj.event == 'deleteNotice') {
                alert("删除？");
                deleteNotice(data.notice_id);
            }
        });

        //删除公告
        function deleteNotice(notice_id){
            $.ajax({
                url:"/bbs/deleteNotice.action",
                type:"POST",
                data:{'notice_id':notice_id},
                dataType:'JSON',
                cache:false,
                success:function (data) {
                    if(data.code==200){
                        alert("删除成功!");
                        //重载表格
                        reloadNoticeTable();
                    }else{
                        alert(data.msg);
                    }
                } ,
                error:function () {
                    alert("删除失败.")
                }
            });
        };

    });
})